package com.caine.ts.ch22;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.caine_ts.R;


public class Lesson22 extends Activity {
	
	private Timer timer = null;
	private ImageView img = null;
	private ClipDrawable clipDraw = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lesson22);
		init();

	}


	void init() {
	  img = (ImageView) findViewById(R.id.img_1);
	  clipDraw = (ClipDrawable) img.getDrawable();
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0x111) {				
				clipDraw.setLevel(clipDraw.getLevel()+200);
			}
		};
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		timer.cancel();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		timer.cancel();
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message message = new Message();
				message.what = 0x111;
				System.out
						.println("handler.sendMessage("+message+"); "+"clipDraw Level()"+clipDraw.getLevel());
				handler.sendMessage(message);
				if(clipDraw.getLevel() >= 10000){
					timer.cancel();
				}
			}
		}, 0, 400);
	}

}
